package com.zsix.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zsix.common.entity.dictData.DictDataEntity;
import com.zsix.common.entity.taskAssignee.ApprovalTrack;
import com.zsix.common.entity.taskAssignee.GenerWork;
import com.zsix.common.util.StringUtils;
import com.zsix.system.mapper.SqlMapper;
import com.zsix.system.mapper.dict.DictMapper;
import com.zsix.system.mapper.taskAssigneeMapper.ApprovalTrackMapper;
import com.zsix.system.mapper.taskAssigneeMapper.GenerWorkMapper;
import com.zsix.system.service.IWordService;
import com.zsix.system.service.manager.ApprovalTrackManager;
import com.zsix.system.service.manager.WordTemplate;
import com.zsix.system.strategy.dict.DictContext;
import com.zsix.system.strategy.dict.DictStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.util.*;

@Service
public class WordServiceImpl implements IWordService {
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private ApprovalTrackMapper approvalTrackMapper;
    @Autowired
    private GenerWorkMapper generWorkMapper;
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictContext dictContext;
    @Value("${pdf.inputPath}")
    private  String inputPath;
    @Value("${pdf.oputPath}")
    private  String oputPath;
    @Value("${pdf.kkfileUrl}")
    private  String kkfileUrl;
    @Value("${auto.host_uri}")
    private  String host_uri;

    /**
     * 打印demo
     */
    @Override
    public void PrintDoc() {
        List<Map<Object,Object>> list=new ArrayList<>();
        String filePath=inputPath+"/打印测试.docx";
        String outFielPath= oputPath+"/结果.docx";;
        String sql="select * from sys_user";
        Map<String,Object> map=sqlMapper.single(sql,null);
        WordTemplate.printDoc(filePath,outFielPath,map,list);
        String jcfaUrl=host_uri+"print/prieviewFile?fileName="+map.get("id").toString()+"/打印测试.docx&fullfilename=打印测试.docx";
        List<HashMap<String,Object>> fileList=new ArrayList<>();
        HashMap<String,Object> jcfaFile=new HashMap<>();
        jcfaFile.put("fileName","打印测试");
        jcfaFile.put("fileUrl",kkfileUrl+"?url="+URLEncoder.encode(Base64.getEncoder().encodeToString(jcfaUrl.getBytes())));
        jcfaFile.put("kkfileUrl",kkfileUrl);
        fileList.add(jcfaFile);
    }
    @Override
    public void prieviewFile(HttpServletResponse response,String fileName){
        try{
            // 可使用ServletContext 通过文件名获取 媒体资源类型
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline;filename="+fileName);
            File file = new File(oputPath+"/" + fileName);

            ServletOutputStream ros = response.getOutputStream();

            // 可参考: StreamUtils
            FileInputStream fis = new FileInputStream(file);
            byte[] bytes = new byte[4 * 1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                ros.write(bytes, 0, len);
            }

            ros.flush();
            ros.close();
            fis.close();
        }
        catch (Exception ex){

        }
    }
}
